#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using Premiere.Window;
using System.Collections;
using Gizmox.WebGUI;

#endregion

namespace PremiereMX
{
    public partial class UserControlExchangeRate : UserControl, IUserControlExchangeRate
    {
        ExchangeRateLogic m_logic;
        IMainForm m_mainForm;
        Hashtable m_items = new Hashtable();

        public UserControlExchangeRate(string name, IMainForm mainForm)
        {
            InitializeComponent();
            m_logic = new ExchangeRateLogic(mainForm, this);
            m_mainForm = mainForm;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            InitButtonClick();
            dataGridViewExchange.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewExchange_CellValidating);
            dataGridViewExchange.CellValidated += new DataGridViewCellEventHandler(dataGridViewExchange_CellValidated);
            dataGridViewExchange.CellValueChanged += new DataGridViewCellEventHandler(dataGridViewExchange_CellValueChanged);
        }

        void dataGridViewExchange_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex != 0) && (e.ColumnIndex != 3))
            {
                ExchangeRateDTO dto = (ExchangeRateDTO)dataGridViewExchange.Rows[e.RowIndex].Tag;
                decimal value = Convert.ToDecimal(dataGridViewExchange[e.ColumnIndex, e.RowIndex].Value);
                value = Math.Round(value, dto.Currency.DecimalPlace);
                switch (e.ColumnIndex)
                {
                    case 1:
                        dto.BuyRate = value;
                        break;
                    case 2:
                        dto.SellRate = value;
                        break;
                }
                dataGridViewExchange[e.ColumnIndex, e.RowIndex].Value = value;
            }
        }

        void dataGridViewExchange_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewExchange.Rows[e.RowIndex].ErrorText = null;
        }

        void dataGridViewExchange_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int testint;
            if (e.ColumnIndex != 0)
            {
                try
                {
                    if (e.FormattedValue.ToString().Length != 0)
                    {
                        // Try to convert the cell value to an int.
                        //if (!CommonUtils.TryParse(e.FormattedValue.ToString(), out testint))
                        //{
                        //    dataGridViewExchange.Rows[e.RowIndex].ErrorText =
                        //        "Error: Invalid entry";
                        //    e.Cancel = true;
                        //}
                        Convert.ToDecimal(e.FormattedValue);
                        ExchangeRateDTO dto = (ExchangeRateDTO)dataGridViewExchange.Rows[e.RowIndex].Tag;
                        if (dto == null)
                        {
                            dataGridViewExchange.Rows[e.RowIndex].ErrorText =
                                                      "Error: Can not entry";
                            e.Cancel = true;
                        }
                    }
                }
                catch (Exception x)
                {
                    dataGridViewExchange.Rows[e.RowIndex].ErrorText ="Error: Invalid entry";
                    e.Cancel = true;
                }

            }
        }
        private void InitButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonPrint.Click += new EventHandler(toolBarButtonPrint_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
        }
        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        void toolBarButtonPrint_Click(object sender, EventArgs e)
        {
            m_logic.Print();
        }

        void toolBarButtonNew_Click(object sender, EventArgs e)
        {
            m_logic.New();
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            m_logic.Edit();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }
        void DeleteConfirmation(object sender, EventArgs e)
        {
            MessageBoxWindow msg = (MessageBoxWindow)sender;
            if (msg.DialogResult == DialogResult.Yes)
                m_logic.Delete();
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            m_logic.Save();
        }
        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (listViewRecords.SelectedItem == null) return;
            //DivisionDTO user = (DivisionDTO)listViewRecords.SelectedItem.Tag;
            //m_logic.UpdateSubject(user);
        }


        #region IUserControlDivision Members

        public DateTime LastUpdate
        {
            set {  }
        }

        public string ComputerName
        {
            set { }
        }

        public string UserUpdateBy
        {
            set {  }
        }

        public void ShowMessage(string mess)
        {
            MessageBox.Show(mess, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void SetMainForm(IMainForm mainform)
        {
            m_mainForm = mainform;
        }

        public void UpdateButton()
        {
            m_mainForm.UpdateButton(toolBarMain.Buttons);
        }

        public void Init()
        {
            m_logic.Init();
        }

        public void Refresh()
        {
            m_logic.Init();
            m_logic.Refresh();
        }

        public void ClearForm()
        {

        }

        public void SetToReadOnly(bool ro)
        {
            dataGridViewTextBoxColumnBuyRate.ReadOnly = ro;
            dataGridViewTextBoxColumnMiddleRate.ReadOnly = ro;
            dataGridViewTextBoxColumnSellRate.ReadOnly = ro;
            buttonCopyFromPrev.Enabled = !ro;
        }

        public void SetButtonEditMode(Premiere.Window.EditMode editMode)
        {
            toolBarButtonSave.Enabled = ((editMode.Equals(EditMode.Edit) || editMode.Equals(EditMode.Insert))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonEdit.Enabled = ((editMode.Equals(EditMode.OK))) && m_mainForm.GetRules(this.Name).Edit;
            toolBarButtonPrint.Enabled = m_mainForm.GetRules(this.Name).Print;
        }

        public void RemoveItem(long id)
        {

        }

        public DateTime Date
        {
            get
            {
                return dateTimePickerDate.Value;
            }
            set
            {
                dateTimePickerDate.Value = value;
            }
        }

        public void AddExchangeRate(ExchangeRateDTO dto)
        {
            DataGridViewRow rw = dataGridViewExchange.Rows[dataGridViewExchange.Rows.Add(
                dto.Currency.Code,0,0,0
                )];
            rw.Tag = dto;
            dataGridViewExchange[1, rw.Index].Style.Format = "N" + dto.Currency.DecimalPlace;// = new DataGridViewCellStyle();
            dataGridViewExchange[2, rw.Index].Style.Format = "N" + dto.Currency.DecimalPlace;
            dataGridViewExchange[3, rw.Index].Style.Format = "N" + dto.Currency.DecimalPlace;
            dataGridViewExchange[1, rw.Index].Value = dto.BuyRate;
            dataGridViewExchange[2, rw.Index].Value = dto.SellRate;
            dataGridViewExchange[3, rw.Index].Value = dto.MiddleRate;
            
        }

        public void AddExchangeRateCopy(ExchangeRateDTO dto)
        {
            foreach (DataGridViewRow rw in dataGridViewExchange.Rows)
            {
                if (rw.Tag == null) continue;
                ExchangeRateDTO x = (ExchangeRateDTO)rw.Tag;
                if (x.Currency.Id == dto.Currency.Id)
                {
                    x.BuyRate = dto.BuyRate;
                    x.SellRate = dto.SellRate;
                    x.MiddleRate = dto.MiddleRate;
                    dataGridViewExchange[1, rw.Index].Value = dto.BuyRate;
                    dataGridViewExchange[2, rw.Index].Value = dto.SellRate;
                    dataGridViewExchange[3, rw.Index].Value = dto.MiddleRate;
                }
            }

        }

        public void ClearItems()
        {
            dataGridViewExchange.Rows.Clear();
        }

        public IList GetUpdatedItems()
        {
            IList list = new ArrayList();
            foreach (DataGridViewRow rw in dataGridViewExchange.Rows)
            {
                if (rw.Tag == null) continue;
                ExchangeRateDTO dto = (ExchangeRateDTO)rw.Tag;
                dto.Date = Date;
                dto.BuyRate = Convert.ToDecimal(dataGridViewExchange[1, rw.Index].Value);
                dto.SellRate = Convert.ToDecimal(dataGridViewExchange[2, rw.Index].Value);
                dto.MiddleRate = Convert.ToDecimal(dataGridViewExchange[3, rw.Index].Value);
                list.Add(dto);
            }
            return list;
        }

        public void UpdateList(IList list)
        {
            throw new NotImplementedException();
        }

        public void FocusToCode()
        {

        }
        public bool IsValidSubject()
        {
            return true;
        }


        #endregion

        private void dateTimePickerDate_ValueChanged(object sender, EventArgs e)
        {
            m_logic.ChangeDate(dateTimePickerDate.Value);
        }

        private void buttonprev_Click(object sender, EventArgs e)
        {
            dateTimePickerDate.Value = dateTimePickerDate.Value.Subtract(new TimeSpan(1, 0, 0, 0));
        }

        private void buttonnext_Click(object sender, EventArgs e)
        {
            dateTimePickerDate.Value = dateTimePickerDate.Value.AddDays(1);
        }

        private void buttonCopyFromPrev_Click(object sender, EventArgs e)
        {
            m_logic.CopyFromDate(dateTimePickerDate.Value.Subtract(new TimeSpan(1, 0, 0, 0)));
        }
    }
}